<?
require_once("mvc/action/Action.php");
require_once("cms/contact/om/ContactPeer.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $errors = new ActionMessages();

        $contact =& $form->getContact();
        $contact->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            // process image
            $formImage =& $form->getFormImage();

            if(!$form->getDeleteImage() && $formImage["size"] > 0) {

                $image = $contact->getImageRelatedByImageId();
                $thumb = $contact->getImageRelatedByThumbnailId();

                require_once("cms/core/util/ImageProperties.php");
                $properties = new ImageProperties(
                    $request->getContext()->getInitParameter(_Contact::SETTINGS)
                );

                require_once("cms/core/util/ImageProcessor.php");
                ImageProcessor::process(
                    $conn, $errors, $formImage,
                    $properties, $image, $thumb
                );

                // Set the generated ids
                $contact->setThumbnailId($thumb->getImageId());
                $contact->setImageId($image->getImageId());

            } else if($form->getDeleteImage()) {

                $image = $contact->getImageRelatedByImageId();
                $thumb = $contact->getImageRelatedByThumbnailId();

                require_once("cms/core/om/BinaryFilePeer.php");

                BinaryFilePeer::doDelete($thumb->getBinaryFile(), $conn);
                BinaryFilePeer::doDelete($image->getBinaryFile(), $conn);
            }

            if($contact->getContactId() > 0) {

                ContactPeer::doUpdate($contact, $conn);

            } else {

                $contact->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                $contact->setActive(_Core::AUTO_ACTIVATE);
                ContactPeer::doInsert($contact, $conn);

            }

            $conn->commit();
            
        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.save"));
            $this->log->error($e->getMessage());
        }

        // Return any save errors.
        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }


        // Reset the contactListForm from & to session.
        $session  = $request->getSession();
        $listForm = $session->getAttribute("cms/contactListForm");
        $listForm->setPage(1);
        $session->setAttribute("cms/contactListForm", $listForm);
        
        
        // Add contact to search index
        if($request->getContext()->getInitParameter(_Core::SEARCH) === true) {
        	$lang = $request->getContext()->getInitParameter(
                _Core::DEFAULT_LANGUAGE);   
            $contact->addToIndex($request->getContext()->
                getInitParameter(_Core::SEARCH_INDEX_PATH), $lang);
        }
        
        
        if($request->getContext()->getInitParameter(_Core::CACHE)) {
            // Get the cache
            $cache = $request->getContext()->getCache();             
            // Clear the page cache
            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
        }

        
        // Return to index
        return $mapping->findForward("success");

    }

}
?>